package com.joey.sf.no1;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;

public class PaintUtils {
    public static void main(String[] args) {
//       paint1();
//       paint2();
       paint3();
    }

    //画抛物线曲线
    public static void paint1() {
        JFrame jFrame = new JFrame();
        JPanel jPanel = new JPanel() {
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                g.setColor(Color.BLACK);
                int maxX = 100;
                for (int i = 0; i < maxX; i++) {
                    g.drawOval(i,500-i,1,1);
                    g.drawOval(i,500-i*i,1,1);
                    g.drawOval(i, 500-(int) (i*Math.log(i)),1,1);
                }
            }
        };
        jFrame.add(jPanel);
        jFrame.setBounds(0, 0, 500, 500);
        jFrame.setVisible(true);
    }

    //柱状图 随机数组
    public static void paint2() {
        JFrame jFrame = new JFrame();
        int[] valueArray = new int[50];
        for (int i = 0; i < valueArray.length; i++) {
            valueArray[i] = (int) (Math.random() * 400);
        }
        JPanel jPanel = new JPanel() {
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                for (int i = 0; i < valueArray.length; i++) {
                    g.drawRect(i*8,400 - valueArray[i],5,valueArray[i]);
                }
            }
        };
        jFrame.add(jPanel);
        jFrame.setBounds(0, 0, 500, 500);
        jFrame.setVisible(true);
    }

    //柱状图 随机数组
    public static void paint3() {
        JFrame jFrame = new JFrame();
        int[] valueArray = new int[50];
        for (int i = 0; i < valueArray.length; i++) {
            valueArray[i] = (int) (Math.random() * 400);
        }
        //排序后
        Arrays.sort(valueArray);
        JPanel jPanel = new JPanel() {
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                for (int i = 0; i < valueArray.length; i++) {
                    g.drawRect(i*8,400 - valueArray[i],5,valueArray[i]);
                }
            }
        };
        jFrame.add(jPanel);
        jFrame.setBounds(0, 0, 500, 500);
        jFrame.setVisible(true);
    }
}
